Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: select random free server ports with 0 #960

Merged
merged 1 commit into from
Sep 24, 2024

Conversation

jjaakola-aiven
Copy link
Contributor

@jjaakola-aiven jjaakola-aiven commented Sep 24, 2024

About this change - What it does

The previous port selection algorithm relied on port range. The range
was from 48700 to 49000. This is 300 ports of which some may be already
reserved in the system running tests. The free ports are allocated between
the pytest workers. With 4 workers there is 75 ports for each. This causes
flakiness in the tests as ports may not be available or are still in
wait after previous tests. In the test environment it is not necessary
to select ports from IANA dynamic port assignment range. Any free port
is ok and port 0 can be used for dynamically allocated free port.

@jjaakola-aiven jjaakola-aiven force-pushed the jjaakola-aiven-select-server-ports-with-0-in-tests branch from 8c8bc67 to 82726da Compare September 24, 2024 06:49
The previous port selection algorithm relied on port range. The range
was from 48700 to 49000. This is 300 ports of which some may be already
reserved in the system running tests. The free ports are allocated between
the pytest workers. With 4 workers there is 75 ports for each. This causes
flakiness in the tests as ports may not be available or are still in
wait after previous tests. In the test environment it is not necessary
to select ports from IANA dynamic port assignment range. Any free port
is ok and port 0 can be used for dynamically allocated free port.
@jjaakola-aiven jjaakola-aiven force-pushed the jjaakola-aiven-select-server-ports-with-0-in-tests branch from 82726da to 3e2202e Compare September 24, 2024 07:37
Copy link

Coverage report

This PR does not seem to contain any modification to coverable code.

@jjaakola-aiven jjaakola-aiven marked this pull request as ready for review September 24, 2024 08:33
@jjaakola-aiven jjaakola-aiven requested review from a team as code owners September 24, 2024 08:33
@@ -15,8 +14,15 @@
import socket


@contextmanager
def allocate_port_no_reuse() -> Iterator[int]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit-pick]
Maybe Generator[int] as the return type, since you already did earlier, but doesn't hurt as generators as also iterators.

def allocate_port_no_reuse() -> Iterator[int]:
"""Allocate random free port and do not allow reuse."""
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock:
sock.bind(("127.0.0.1", 0))
Copy link
Contributor

@nosahama nosahama Sep 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If host or port are ‘’ or 0 respectively the OS default behavior will be used.

Simpler and nice 👍

"""Allocate random free port."""
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock:
sock.bind(("127.0.0.1", 0))
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The SO_REUSEADDR flag tells the kernel to reuse a local socket in TIME_WAIT state, without waiting for its natural timeout to expire.

@nosahama nosahama merged commit a74421a into main Sep 24, 2024
10 checks passed
@nosahama nosahama deleted the jjaakola-aiven-select-server-ports-with-0-in-tests branch September 24, 2024 09:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants